package 滑动窗口;

import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName : _3_无重复字符的最长子串
 * @Author : 骆发茂
 * @Date: 2022/6/12 8:51
 * @Description :
 */
public class _3_无重复字符的最长子串 {
    public int lengthOfLongestSubstring(String s) {
        int left= 0, right= 0, length= 0, Maxlength = 0;
        Set<Character> set1 = new HashSet<>();

        while (right < s.length()) {
            //set存储的是不包含重复的，如果里面没有重复的，就是满足条件，继续R向右
            if (!set1.contains(s.charAt(right))) {
                set1.add(s.charAt(right));
                length++;
                if (length > Maxlength) {
                    Maxlength = length;
                }
                right++;
            } //不满足情况，L向右
            else {
                while (set1.contains(s.charAt(right))) {
                    set1.remove(s.charAt(left));
                    left++;
                    length--;

                }
                set1.add(s.charAt(right));
                length++;
                right++;


            }
        }
        return Maxlength;
    }
}